import axios from 'axios'
import nprogress from 'nprogress'
import 'nprogress/nprogress.css'
import store from '@/store'

// 实例化
const http = axios.create({
    baseURL: 'http://39.98.123.211',
    timeout: 5000,
})

// 请求拦截器
http.interceptors.request.use((config) => {
    if (store.state.detail.uuid_token) {
        // 如果有游客uuid，就加个请求头字段
        config.headers.userTempId = store.state.detail.uuid_token
    }

    // 携带token
    if (localStorage.getItem('TOKEN')) {
        config.headers.token = localStorage.getItem('TOKEN')
    }
    nprogress.start()
    return config
})

// 响应拦截器
http.interceptors.response.use((res) => {
    nprogress.done()
    return res.data
}, (error) => {
    return Promise.reject(error);
})

export default http